//
//  ZZXDragTV.m
//  Created by on 2021/8/10.
//

#import "ZZXDragTV.h"
@interface ZZXDragTV()
@property (nonatomic,assign)NSDragOperation dragOperation;
@end

@implementation ZZXDragTV
- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    // Drawing code here.
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self registerForDraggedTypes:@[NSPasteboardTypeFileURL]];
}

- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
    NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
    NSPasteboard* pasteboard = [sender draggingPasteboard];
    self.dragOperation = NSDragOperationNone;
    if ([[pasteboard types] containsObject:NSPasteboardTypeFileURL]) {
        if (sourceDragMask & NSDragOperationLink) {
            self.dragOperation = NSDragOperationLink;
        } else if (sourceDragMask & NSDragOperationCopy) {
            self.dragOperation = NSDragOperationCopy;
        }
    }
    //开始拖放,返回拖放类型
    return self.dragOperation;
}

//忽略未使用参数的警告
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
- (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender {
    return self.dragOperation;
}
#pragma clang diagnostic pop

- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
    NSPasteboard* pasteboard = [sender draggingPasteboard];
    if ([[pasteboard types] containsObject:NSPasteboardTypeFileURL]) {
        NSArray<Class> *classes = @[[NSURL class]];
        NSDictionary *options = @{};
        NSArray<NSURL*> *files = [pasteboard readObjectsForClasses:classes options:options];
        NSMutableArray* fileUrls = @[].mutableCopy;
        NSFileManager* fileManager = [NSFileManager defaultManager];
        for (NSURL *url in files) {
            BOOL isDiretory = YES;
            BOOL isExists = [fileManager fileExistsAtPath:url.path isDirectory:&isDiretory];
            if (isExists || isDiretory) {
                [fileUrls addObject:[NSURL fileURLWithPath:url.path]];
            }
        }
        [self.dragDelegate tableView:self dragFilesInToViewComplete:fileUrls.copy];
    }
    return YES;
}
@end
